#include "hal_periph_clk.h"
#include "driverlib.h"

bool hal_periph_clk_enable(hal_periph_t periph)
{
    static const SysCtl_PeripheralPCLOCKCR periph_tbl[] = {
        SYSCTL_PERIPH_CLK_TIMER0,
        SYSCTL_PERIPH_CLK_TIMER1,
        SYSCTL_PERIPH_CLK_TIMER2,
        SYSCTL_PERIPH_CLK_HRCAL,
        SYSCTL_PERIPH_CLK_TBCLKSYNC,
        SYSCTL_PERIPH_CLK_EPWM1,
        SYSCTL_PERIPH_CLK_EPWM2,
        SYSCTL_PERIPH_CLK_EPWM3,
        SYSCTL_PERIPH_CLK_EPWM4,
        SYSCTL_PERIPH_CLK_EPWM5,
        SYSCTL_PERIPH_CLK_EPWM6,
        SYSCTL_PERIPH_CLK_EPWM7,
        SYSCTL_PERIPH_CLK_ECAP1,
        SYSCTL_PERIPH_CLK_ECAP2,
        SYSCTL_PERIPH_CLK_ECAP3,
        SYSCTL_PERIPH_CLK_EQEP1,
        SYSCTL_PERIPH_CLK_EQEP2,
        SYSCTL_PERIPH_CLK_SCIA,
        SYSCTL_PERIPH_CLK_SCIB,
        SYSCTL_PERIPH_CLK_SCIC,
        SYSCTL_PERIPH_CLK_SPIA,
        SYSCTL_PERIPH_CLK_I2CA,
        SYSCTL_PERIPH_CLK_I2CB,
        SYSCTL_PERIPH_CLK_CANA,
        SYSCTL_PERIPH_CLK_MCANA,
        SYSCTL_PERIPH_CLK_ADCA,
        SYSCTL_PERIPH_CLK_ADCC,
        SYSCTL_PERIPH_CLK_CMPSS1,
        SYSCTL_PERIPH_CLK_CMPSS2,
        SYSCTL_PERIPH_CLK_CMPSS3,
        SYSCTL_PERIPH_CLK_CMPSS4,
        SYSCTL_PERIPH_CLK_LINA,
        SYSCTL_PERIPH_CLK_PMBUSA,
        SYSCTL_PERIPH_CLK_DCC0,
        SYSCTL_PERIPH_CLK_EPG1
    };

    if (periph >= HAL_PERIPH_MAX) return false;
    SysCtl_enablePeripheral(periph_tbl[periph]);
    return true;
}

bool hal_periph_clk_disable(hal_periph_t periph)
{
    static const SysCtl_PeripheralPCLOCKCR periph_tbl[] = {
        SYSCTL_PERIPH_CLK_TIMER0,
        SYSCTL_PERIPH_CLK_TIMER1,
        SYSCTL_PERIPH_CLK_TIMER2,
        SYSCTL_PERIPH_CLK_HRCAL,
        SYSCTL_PERIPH_CLK_TBCLKSYNC,
        SYSCTL_PERIPH_CLK_EPWM1,
        SYSCTL_PERIPH_CLK_EPWM2,
        SYSCTL_PERIPH_CLK_EPWM3,
        SYSCTL_PERIPH_CLK_EPWM4,
        SYSCTL_PERIPH_CLK_EPWM5,
        SYSCTL_PERIPH_CLK_EPWM6,
        SYSCTL_PERIPH_CLK_EPWM7,
        SYSCTL_PERIPH_CLK_ECAP1,
        SYSCTL_PERIPH_CLK_ECAP2,
        SYSCTL_PERIPH_CLK_ECAP3,
        SYSCTL_PERIPH_CLK_EQEP1,
        SYSCTL_PERIPH_CLK_EQEP2,
        SYSCTL_PERIPH_CLK_SCIA,
        SYSCTL_PERIPH_CLK_SCIB,
        SYSCTL_PERIPH_CLK_SCIC,
        SYSCTL_PERIPH_CLK_SPIA,
        SYSCTL_PERIPH_CLK_I2CA,
        SYSCTL_PERIPH_CLK_I2CB,
        SYSCTL_PERIPH_CLK_CANA,
        SYSCTL_PERIPH_CLK_MCANA,
        SYSCTL_PERIPH_CLK_ADCA,
        SYSCTL_PERIPH_CLK_ADCC,
        SYSCTL_PERIPH_CLK_CMPSS1,
        SYSCTL_PERIPH_CLK_CMPSS2,
        SYSCTL_PERIPH_CLK_CMPSS3,
        SYSCTL_PERIPH_CLK_CMPSS4,
        SYSCTL_PERIPH_CLK_LINA,
        SYSCTL_PERIPH_CLK_PMBUSA,
        SYSCTL_PERIPH_CLK_DCC0,
        SYSCTL_PERIPH_CLK_EPG1
    };

    if (periph >= HAL_PERIPH_MAX) return false;
    SysCtl_disablePeripheral(periph_tbl[periph]);
    return true;
}

void hal_periph_clk_enable_all(void)
{
    for (hal_periph_t p = HAL_PERIPH_TIMER0; p < HAL_PERIPH_MAX; ++p) {
        hal_periph_clk_enable(p);
    }
}
